package cn.it.hbtt.dao;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class BaseDAO {
	private  Connection  conn;
	private  PreparedStatement pstm;
	private  ResultSet rs;
	private static Properties pro=null;
		
	
	
	public static  Connection  getConnection() {	

		try {

			/*String url = pro.getProperty("url");
			String user = pro.getProperty("user");
			String password = pro.getProperty("password");*/
			String url = "jdbc:mysql://localhost:3306/easybuy?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
			String user = "root";
			String password = "123";
			
			Class.forName("com.mysql.cj.jdbc.Driver");

			
			return DriverManager.getConnection(url, user, password);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void main(String[] args) {
		Connection conn=getConnection();
		System.out.println(conn);
	}
	
	public static void  closeAll(Connection conn,Statement stm,ResultSet rs) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(stm!=null) {
			try {
				stm.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		if(conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//通用增删改
	public int  executeUpdate(String  sql,Object[] args) {
		int result=0;
		
		try {
			conn=getConnection();
			// select  *  from  student where name=? and password=?
			pstm=conn.prepareStatement(sql);
			if(args!=null) {
				for (int i = 0; i < args.length; i++) {
					pstm.setObject(i+1, args[i]);
				}
			}
			result=pstm.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}

}
